var app = angular.module('zhijiaoAdminApp.controllers', []);

app.controller('mainCtr', ['$rootScope', '$scope', '$location', 'session', 'ajaxService', 'toastPCService',
    function ($rootScope, $scope, $location, session, ajax, toast) {

        $rootScope.mainData = {
            user: window._admin,
            lang: [{id: '1', value: '中文'}, {id: '2', value: '英文'}]
        };

        $rootScope.childPageScope = {};
        $rootScope.getListUrl = '';

        $rootScope.ActiveTabMenu = session.get('ActiveTabMenu') || 'HomePage';
        $rootScope.TabMenu = session.get('TabMenu') || [
            {
                name: '首页',
                url: '/index/home/',
                access: 'HomePage',
                default: true
            }
        ];
        $rootScope.menuStatus = function () {
            session.set('TabMenu', $rootScope.TabMenu);
            session.set('ActiveTabMenu', $rootScope.ActiveTabMenu);
        }

        //关闭某个tab菜单
        $scope.closeTabMenu = function (index) {
            $rootScope.TabMenu.splice(index, 1);
            $rootScope.menuStatus();
        }
        //关闭全部tab菜单
        $scope.closeAllTabMenu = function () {
            $rootScope.ActiveTabMenu = 'HomePage';
            $rootScope.TabMenu = [
                {
                    name: '首页',
                    url: '/index/home/',
                    access: 'HomePage',
                    default: true
                }
            ];
            $rootScope.menuStatus();
            $scope.location('/index/home');
        }
        //关闭其它tab菜单
        $scope.closeOtherTabMenu = function () {
            var tabMenu = [];
            $rootScope.TabMenu.forEach(function (item, index) {
                if (item.access == $rootScope.ActiveTabMenu || item.default) {
                    tabMenu.push(item);
                }
            })
            $rootScope.TabMenu = tabMenu;
            $rootScope.menuStatus();
        }

        //切换tab
        $scope.tabChange = function (index) {
            if ($rootScope.TabMenu[index].access == $rootScope.ActiveTabMenu) {
                return;
            }
            $rootScope.ActiveTabMenu = $rootScope.TabMenu[index].access;
            $rootScope.menuStatus();
            $scope.location($rootScope.TabMenu[index].url);
        }


        $scope.showUpdatePwd = function () {
            $scope.updatePwd = {
                old: '',
                new: ''
            };
            $('#updatePwd').modal("show")
        }

        $scope.commitUpdatepwd = function () {
            ajax('updatePwd', $scope.updatePwd, function (rel) {
                if (rel.code) {
                    toast.show(2, rel.message);
                } else {
                    toast.show(1, '修改成功');
                    $('#updatePwd').modal('hide');
                }
            })
        }

        $scope.getList = function (page) {
            var scope = $rootScope.childPageScope;
            if (page) {
                if (page == 1)
                    scope.data.list = [];
                scope.data.search.page = page;
            }
            scope.data.loading = true;
            ajax($rootScope.getListUrl, scope.data.search, function (rel) {
                scope.data.loading = false;
                scope.data.list = rel.data.list;
                scope.data.pages = rel.data.pages;
                scope.data.count = rel.data.count;
                if (scope.getListSuccess) {
                    scope.getListSuccess(rel.data);
                }
            })
        }

        $scope.getItemIndex = function ($index, $page, $pageSize) {
            return ($page - 1) * $pageSize + $index + 1
        }

        $scope.go_back = function () {
            history.go(-1)
        }

    }])